package com.hibiscusking.org.controller;

import com.hibiscusking.basic.anno.PreAuthorize;
import com.hibiscusking.basic.util.AjaxResult;
import com.hibiscusking.basic.util.PageList;
import com.hibiscusking.org.domain.Employee;
import com.hibiscusking.org.query.EmployeeQuery;
import com.hibiscusking.org.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@PreAuthorize
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PutMapping
    @PreAuthorize(name = "员工添加或者修改",sn = "employee:add")
    public AjaxResult add(@RequestBody Employee employee) {
        if (employee.getId() == null) {
            employeeService.add(employee);
        } else {
            employeeService.update(employee);
        }
        return AjaxResult.me().success();
    }
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "员工删除",sn = "employee:dele")
    public AjaxResult dele(@PathVariable("id") Long id) {
        employeeService.dele(id);
        return AjaxResult.me().success();
    }
    @GetMapping
    @PreAuthorize(name = "员工查询全部",sn = "employee:findAll")
    public List<Employee> findAll() {
        return employeeService.findAll();
    }
    /** 根据id查询一个 */
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id查询单个员工",sn = "employee:findById")
    public Employee findById(@PathVariable("id") Long id){
        return employeeService.findById(id);
    }

    /** 分页查询 */
    @PostMapping
    @PreAuthorize(name = "分页查询员工",sn = "employee:queryByPage")
    public PageList<Employee> queryByPage(@RequestBody EmployeeQuery query){
        return employeeService.queryByPage(query);
    }
    @PatchMapping
    @PreAuthorize(name = "批量删除员工",sn = "employee:batchDele")
    public AjaxResult batchDele(@RequestBody List<Long> ids){
        employeeService.batchDele(ids);
        return AjaxResult.me().success();
    }
}
